import java.sql.ResultSet;
import java.sql.SQLException;

import mysql.Mysql;


public class Auxiliar {
	public void login(String email, String pass) {
		Mysql bd = new Mysql();
		String strsql = "select * from tb_users where email = '" + email + "' and password = '"+pass+"'";
		
		ResultSet rs = bd.select(strsql);
		
		try {
			rs.first();			
			try {
				System.out.println(rs.getString("id"));
			} catch (SQLException e) {
				System.out.println("0");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}		
	}
	
	public int joinServer(int game_id, int user_id, int color) {		
		Mysql bd = new Mysql();
		String strsql;
		
		strsql = "insert into tb_games_users (game_id, user_id, gold, color) values (" +
				game_id + "," + user_id + ",1000," + color +")";		
		bd.insertUpdate(strsql);
		
		strsql = "update tb_games set guser_idx = guser_idx + 1, status = 1 where id = " + game_id;
		bd.insertUpdate(strsql);
		
		User user = this.loadUserObj(user_id);
		Map map = this.loadMap(game_id);
		
		this.createComponent(user, map, "castle", 9, 9);
		
		
		return 1;
	}
	
	public int gameStatus(int game_id) {
		Mysql bd = new Mysql();
		String strsql = "select status from tb_games where id = " + game_id;
		
		
		ResultSet rs = bd.select(strsql);		
		try {
			rs.first();			
			try {
				return Integer.parseInt(rs.getString("status"));
			} catch (SQLException e) {
				System.out.println("0");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return 0;
	}
	
	public void listServer(int status) {
		Mysql bd = new Mysql();
		String strsql = "select * from tb_games where guser_idx < 2 and status = " + status;
		
		
		ResultSet rs = bd.select(strsql);		
		try {
			while (rs.next()) {		
				System.out.print(rs.getString("id")+","+rs.getString("name")+";");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void loadUser(int user_id) {
		Mysql bd = new Mysql();
		String strsql = "select * from tb_users where id = " + user_id;
		
		ResultSet rs = bd.select(strsql);		
		try {
			rs.first();			
			try {
				System.out.println(rs.getString("id")+":"+rs.getString("name")+
						":"+rs.getString("email")+":"+rs.getString("password"));
			} catch (SQLException e) {
				System.out.println("0");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public User loadUserObj(int user_id) {
		Mysql bd = new Mysql();
		String strsql = "select * from tb_users where id = " + user_id;
		
		ResultSet rs = bd.select(strsql);		
		try {
			rs.first();			
			try {
				User user = new User(rs.getString("name"), rs.getString("email"), rs.getString("password"));
				user.setId(Integer.parseInt(rs.getString("id")));
				return user;
			} catch (SQLException e) {
				System.out.println("0");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public int endTurn(int game_id, User user) {
		Mysql bd = new Mysql();
		String strsql = "select * from tb_games as g where id = " + game_id;
		
		ResultSet rs = bd.select(strsql);		
		try {
			rs.first();			
			try {
				int time = (Integer.parseInt(rs.getString("guser_time"))+1)%2;				
				strsql = "update tb_games set guser_time = " + time + " where id = " + rs.getString("id");
				bd.insertUpdate(strsql);
			} catch (SQLException e) {
				System.out.println("0");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 1;
	}	
	
	public int turnUser(int game_id) {
		Mysql bd = new Mysql();
		String strsql; 
		
		strsql = "select guser_time from tb_games as g where g.id = " + game_id;
		ResultSet rs = bd.select(strsql);		
		try {
			rs.first();			
			try {
				return Integer.parseInt(rs.getString("guser_time"));
			} catch (SQLException e) {
				System.out.println("0");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return 1;
	}
	
	public Map loadMap(int map_id) {
		Mysql bd = new Mysql();
		String strsql = "select m.* from tb_games as g " +
						"inner join tb_maps as m on m.id = g.map_id " +
						"where g.id =" + map_id;
		
		ResultSet rs = bd.select(strsql);		
		try {
			rs.first();			
			try {
				Map map = new Map(Integer.parseInt(rs.getString("size")));
				map.setId(Integer.parseInt(rs.getString("id")));
				map.loadTiles();
				return map;
			} catch (SQLException e) {
				System.out.println("0");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Component loadComponent(int comp_id) {
		Mysql bd = new Mysql();
		String strsql = "select * from tb_components where id =" + comp_id;
		
		ResultSet rs = bd.select(strsql);		
		try {
			rs.first();			
			try {
				User user = this.loadUserObj(Integer.parseInt(rs.getString("user_id")));				
				Component c = new Component(rs.getString("type"), user);
				c.setId(Integer.parseInt(rs.getString("id")));
				c.setDamage(Integer.parseInt(rs.getString("damage")));
				c.setName(rs.getString("name"));
				
				return c;
			} catch (SQLException e) {
				System.out.println("0");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;		
	}
	
	public int createComponent(User user, Map map, String type, int x, int y) {
		Component c = new Component(type, user);
		c.setUser(user);
		c.save();


		map.setComponent(c, x, y);
		map.saveTile(x, y);
		
		return 1;
	}	
	
	public int moveComponent(Map map, Component c, int x, int y) {
		Mysql bd = new Mysql();
		map.loadTiles();
		Tile t = map.getTileComponent(c);
		String strsql = "update tb_tiles set x = " + x + ", y = " + y +
						" where id = " + t.getId();
		
		bd.insertUpdate(strsql);
		
		return 1;
	}
	
	public int attack(User user, Map map, Component c, int x, int y) {
		Component c2 = map.getComponent(x, y);
		Mysql bd = new Mysql();		
		String strsql;
		
		if (c.getAttack() >= c2.getLifeRest()) {
			map.loadTiles();
			Tile t = map.getTileComponent(c2);
			
			strsql = "delete from tb_components where id = " + c2.getId();
			bd.insertUpdate(strsql);
			
			strsql = "delete from tb_tiles where id = " + t.getId();
			bd.insertUpdate(strsql);
			
			strsql = "update tb_users set point = point + " + c.getPoint() + " where id = " + user.getId();
		}
		else {
			strsql = "update tb_components set damage = damage + 10 where id = " + c2.getId();
			bd.insertUpdate(strsql);
		}
		
		
		
		
				
		return 1;
	}
	
	public void ranking() {
		Mysql bd = new Mysql();		
		String strsql = "select * from tb_users order by point desc";
		
		ResultSet rs = bd.select(strsql);		
		try {
			while (rs.next()) {		
				System.out.print(rs.getString("name")+","+rs.getString("point")+";");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}		
	}
}
